<?php

load_info_var($cf_controller);           //加载smarty变量
load_file('admin_class', LIB_PATH);
load_file('power_group_class', LIB_PATH);
$thumb_size = array(120, 90);
$self = isset($_POST['self']) ? intval($_POST['self']) : intval($_GET['self']); //是否自己的信息

$main = new Admin($db);
$id = isset($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$gid = isset($_POST['gid']) ? intval($_POST['gid']) : intval($_GET['gid']);
$return_page = $cf_pre_page ? $cf_pre_page : $cf_self_page;
$userauth = new AdminAuth($db);

require_once(INFO_PATH.'cf_var_cate.php');
$cate_array = change_array($cf_var_cate,'id','title');


switch ($cf_action) {

    case "index":
        $smarty->mypage($cf_controller, $cf_action, 0);

        $sindex = isset($_GET['sindex']) ? $_GET['sindex'] : $_POST['sindex'];
        $stype = isset($_POST['stype']) ? $_POST['stype'] : $_GET['stype'];
        $skey = isset($_POST['skey']) ? trim($_POST['skey']) : urldecode(trim($_GET['skey']));
        $skey2 = isset($_POST['skey2']) ? trim($_POST['skey2']) : urldecode(trim($_GET['skey2']));
        $where = "1";

        if ($skey) {
            $where .= " AND admin_name like '%" . $skey . "%'";
        }
        if ($skey2) {
            $where .= " AND admin_true_name like '%" . $skey2 . "%'";
        }
        if ($gid > 1) {
			$group = new PowerGroup($db);
			$srow = $group->get_group_row($gid,"g_left,g_right,g_root,g_club_type,g_club_type");			
            //$where .= " AND admin_gid between '".$srow['g_left']."' AND '".$srow['g_right']."' AND g_root='".$srow['g_root']."'";
			$where .= " AND admin_gid ='".$gid."' AND g_root='".$srow['g_root']."'";
			
        }
		if($self=='1'){
		   $where .= " AND admin_work_status IN ('0','1') ";		   	
		}
		if($self=='2'){
		   $where .= " AND admin_work_status = '2'";		   	
		}
		if(isset($_GET['order'])){
			$order = trim($_GET['order']);
		}else{
		   $order = "admin_id";
		  }
	    $group = $main->get_group_ary(3);
        //echo $where;
      
        load_file('page_class', LIB_PATH);
        $MyPage = new Mypage($db);
       // echo $where;
		$key_word = '?dpt=' . $cf_dpt. '&skey=' . $skey . '&skey2=' . $skey2. '&gid=' . $gid .'&self=' . $self;
		$field="admin_id,admin_name,admin_true_name,admin_sex,admin_check,admin_work_type,admin_work_status,admin_join_date,admin_place,g_title,admin_leaved_date,g_club_type,admin_last_login";
        $data_ary = $MyPage->from_sql($field, 'sys_admin left join sys_group on admin_gid=g_id ', $where, $order." DESC", intval($_GET['cpage']), 20);
        $page = $MyPage->get_page($data, $cf_self_page .$key_word.'&order=' . $order);
		//echo $MyPage->get_sql();
        break;
    case "add":
        if ($cf_ispost) {
            $name = strtolower(trim($_POST['admin_name']));
            if (!$_POST['admin_pwd_new'] or !$_POST['admin_pwd_new2'])
                my_jump('密码不能为空');

            if ($_POST['admin_pwd_new'] != $_POST['admin_pwd_new2'])
                my_jump('两次输入的密码不一样');

            if ($main->check_exist($name))
                my_jump('用户名:' . $name . ' 已存在');
            //if(!check_mail($admin_mail))									//邮件非必须
            //my_jump('The mail is incorrect!');

           	$data = $db->getFormData($main->get_table_name(),$_POST);
			$data['admin_name'] =$name;
			$data['admin_pwd'] =base64_encode(md5($_POST['admin_pwd_new']));
			$data['admin_ip'] =get_ip();
			$data['admin_reg_time'] =time();
			$data['admin_check'] ='1';

			if($_POST['admin_goods_power']){
				$data['admin_goods_power'] = implode(",",$_POST['admin_goods_power']);
			}else{
				$data['admin_goods_power'] = '';
			}
		 
			//$db->debug(1,1);
            $result = $main->insert_data($data);
            if ($result) {
                $main->write_data();
                my_jump('添加成功', '', $cf_self_page.'?dpt='.$cf_dpt, 2);
            } else {
                my_jump('添加失败');
            }
        } else {
            $smarty->mypage($cf_controller, $cf_action, 0);
            $row['power'] = $cf_admin_info['g_act_power'];       //当前权限

            switch ($cf_admin_info['g_root']) {          //内容显示限制
                case HR_DPT:
                    $row['group'] = $main->get_group_ary(3);        //所有组
                    break;
				case ADMINISTRATION_DPT:
                    $row['group'] = $main->get_group_ary(3);        //所有组
                    break;
                default:
                    $row['group'] = $main->get_group_ary();
                    break;
            }

            //所有权限组
            $subtitle = '新增后台用户';
        }

        break;
    case "edit":
        if ($cf_ispost) {

            
			$data = $db->getFormData($main->get_table_name(),$_POST);
			 $data['admin_edit_info'] = $cf_admin_info['admin_name'] . ' ' . date("Y-m-d H:i");
			
		
   

            if ($_POST['admin_pwd_new']) {             //如修改密码
                if ($_POST['admin_pwd_new'] != $_POST['admin_pwd_new2'])
                    my_jump('The confirm password is incorrect');
                $data['admin_pwd'] = base64_encode(md5($_POST['admin_pwd_new']));
            }
			
			
			
			//如果工作状态为离职且已选择离职时间时,以选定的离职时间更新离职时间
			if(intval($_POST['admin_work_status'])==2 && isset($_POST['admin_leaved_date'])){ 
					$data['admin_leaved_date']=$_POST['admin_leaved_date'];	
			}
			
			//如果工作状态为离职且离职时间为空时,获取当时时间更新离职时间
			if(intval($_POST['admin_work_status'])==2 && $_POST['admin_leaved_date']=='0000-00-00' || !isset($_POST['admin_leaved_date'])){
				$data['admin_leaved_date']=date('y-m-d');	
			}
			
			if($_POST['admin_goods_power']){
				$data['admin_goods_power'] = implode(",",$_POST['admin_goods_power']);
			}else{
				$data['admin_goods_power'] = '';
			}
			
		
			//$db->debug(1,1);
            $result = $main->update_data($data, "admin_id='" . $id . "'");
            if ($result) {
                $main->write_data();
                $return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
                unset($_SESSION['pre_url']);
                my_jump('修改成功', '', $return_page.'?dpt='.$cf_dpt, 2);
            } else {
                my_jump('修改失败');
            }
        } else {
            $smarty->mypage($cf_controller, $cf_action, 0);            //修改类型

            $row = $main->get_data_row($id);
            if (!$row)
                my_jump('没有此用户信息');

            $row['power'] = $cf_admin_info['g_act_power'];        //当前权限

 			switch ($cf_admin_info['g_root']) {          //内容显示限制
                case HR_DPT:
                    $row['group'] = $main->get_group_ary(3);        //所有组
					$allow = '1';
                    break;
				case ADMINISTRATION_DPT:
                    $row['group'] = $main->get_group_ary(3);        //所有组
					$allow = '1';
                    break;
                default:
                    $row['group'] = $main->get_group_ary();
					$allow = '0';
                    break;
            }
			
			//$row['group'] = $main->get_group_ary();         //所有权限组
            //my_print($row['group']);

            if ($row['admin_photo']) {
                $photos = explode(",", $row['admin_photo']);
                $count = count($photos);
                for ($i = 0; $i < $count; $i++) {
                    $photos[$i] = UPLOAD_PHOTO_URL . $photos[$i];
                }
                $row['images'] = $photos;
            }

            if ($row['admin_goods_power']) {
				$row['admin_goods_power'] = explode(",",$row['admin_goods_power']);
				
			}
			


            $subtitle = '修改后台用户信息';
            $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
        }
        break;
    case "self":
        if ($cf_ispost) {
           /* $data = array(
                'admin_ename' => trim($_POST['admin_ename']),
                'admin_true_name' => trim($_POST['admin_true_name']),
                'admin_sex' => intval($_POST['admin_sex']),
                'admin_birthday' => trim($_POST['admin_birthday']),
                'admin_country' => trim($_POST['admin_country']),
                'admin_nation' => trim($_POST['admin_nation']),
                'admin_passport' => trim($_POST['admin_passport']),
                'admin_household' => trim($_POST['admin_household']),
                'admin_marry_type' => intval($_POST['admin_marry_type']),
                'admin_mobile' => trim($_POST['admin_mobile']),
                'admin_phone' => trim($_POST['admin_phone']),
                'admin_phone_ext' => trim($_POST['admin_phone_ext']),
                'admin_mail' => trim($_POST['admin_mail']),
                'admin_postal' => trim($_POST['admin_postal']),
                'admin_addr' => trim($_POST['admin_addr']),
                'admin_urgent_name' => trim($_POST['admin_urgent_name']),
                'admin_urgent_phone' => trim($_POST['admin_urgent_phone']),
                'admin_question' => trim($_POST['admin_question']),
                'admin_answer' => trim($_POST['admin_answer']),
                'admin_self_edit' => date("Y-m-d H:i")
            );
			*/
			$data = $db->getFormData($main->get_table_name(),$_POST);
			unset($data['admin_pwd']);
			$data['admin_self_edit'] = date("Y-m-d H:i");
           
            $check = $main->check_data_pwd($cf_admin_info['admin_id'], $_POST['admin_pwd']);
            if (!$check)
                my_jump('你输入的原密码不正确,请重新输入!');


            if ($_POST['admin_pwd_new']) {
                //如修改密码
                if ($_POST['admin_pwd_new'] != $_POST['admin_pwd_new2'])
                    my_jump('两次输入的密码不一致');
                $data['admin_pwd'] = base64_encode(md5($_POST['admin_pwd_new']));
            }

            $result = $main->update_data($data, "admin_id='" . $cf_admin_info['admin_id'] . "'");
            if ($result) {
                //$main->write_data();
                $return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
                unset($_SESSION['pre_url']);
                my_jump('修改成功', '', $cf_self_path . '-act_self.htm?dpt=' . $cf_dpt, 2);
            } else {
                my_jump('修改失败');
            }
        } else {
            $smarty->mypage($cf_controller, $cf_action, 0);
            $id = $cf_admin_info['admin_id'];
            $row = $main->get_data_row($id);
            if (!$row)
                my_jump('没有此用户信息');

            $row['power'] = $cf_admin_info['g_act_power'];        //当前权限
            $row['group'] = $main->get_group_ary(1);         //所有权限组
           
            $subtitle = '修改个人信息';

            $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
        }
        break;
    case "del":
        if ($cf_admin_info['g_act_power'] != 'all')
            my_jump('You do not have permission');
        $row = $main->get_data_row($id);       
        $result = $main->del_data("admin_id='$id'");
        if ($result) {
            $main->write_data();
            my_jump('删除成功', '', $return_page.'?dpt='.$cf_dpt, 1);
        } else {
            my_jump('删除失败');
        }
        break;
    case "delphoto":
        $row = $main->get_data_row($id);
        if (!$row) {
            my_jump('This information does not exist or has been deleted');
        }
        $main->del_photo($row['admin_photo']);
        $result = $main->update_data(array('admin_photo' => ''), "admin_id='" . $id . "'");
        if ($result)
            my_jump('更新完成', '', $return_page.'?dpt='.$cf_dpt);
        else
            my_jump('更新失败');
        break;
    case "set":
        $type = $_GET['type'];
        $value = $_GET['value'];
        $where = "admin_id = '" . $id . "'";
        $result = $main->set_data($value, $type, $where);
        if ($result)
            my_jump('更新完成', '', $return_page.'?dpt='.$cf_dpt, 2);
        else
            my_jump('更新失败');
        break;
    case "all":
        $alltype = $_POST['alltype'];
        $idlist = $_POST['idlist'];
        if ($idlist) {
            $list = implode(",", $idlist);
            switch ($alltype) {
                case "alldel":
                    $result = $main->del_data("admin_id in (" . $list . ") and admin_gid!='1'");
                    break;
                    case "allcheck":
                    $data = array('admin_check' => '1', 'admin_edit_info' => $cf_admin_info['admin_name'] . ' ' . date("y-m-d H:i:s"));
                    $result = $main->update_data($data, "admin_id in (" . $list . ")");
                    break;
                case "alluncheck":
                    $data = array('admin_check' => '0', 'admin_edit_info' => $cf_admin_info['admin_name'] . ' ' . date("y-m-d H:i:s"));
                    $result = $main->update_data($data, "admin_id in (" . $list . ")");
                    break;
				case "allleave":
                    $data = array('admin_work_status'=>'2','admin_check' => '0', 'admin_edit_info' => $cf_admin_info['admin_name'] . ' ' . date("y-m-d H:i:s"));
                    $result = $main->update_data($data, "admin_id in (" . $list . ")");
                    break;
                default:
                    my_jump('请指定操作方式');
                    break;
            }
            if ($result) {
                $main->write_data();
                my_jump('更新完成', '', $return_page.'?dpt='.$cf_dpt, 2);
            } else {
                my_jump('更新失败');
            }
        } else {
            my_jump('输入的参数有误');
        }
        break;
		case "help":
		 $smarty->mypage("crm_help", $cf_action, 0);
		break;
		
		case "exceladmin":
			if ($cf_ispost) {
			require_once('exceladmin.php');
		}else{
			//my_print($data);
			$smarty->assign(array(
				'data_ary' => $data_ary
			));
			$content = $smarty->fetch(MODULE_PATH . "admin/template/exceladmin.htm");
			echo $content;
			exit;
		}
			
		break;
    default:
        my_jump('非法操作!');
        break;
}


$smarty->assign(array(
    'id' => $id,
    'rid' => $rid,
    'gid' => $gid,
    'allow' => $allow,
    'group' => $group,
    'row' => htmlchar($row),
    'data_ary' => $data_ary,
    'sort_ary' => $sort_ary,
    'subtitle' => $subtitle,
    'page' => $page,
	'self' => $self,
	'key_word' => $key_word,
	'skey' => $skey,
	'skey2' => $skey2,
	'cate_array' => $cate_array,
));
$smarty->mydisplay();
?>
